package io.agora.framework;

import android.content.Context;
import android.opengl.GLES20;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;


import com.texeljoy.hteffect.HTEffect;
import com.texeljoy.hteffect.model.HTRotationEnum;

import io.agora.capture.framework.modules.channels.VideoChannel;
import io.agora.capture.framework.modules.processors.IPreprocessor;
import io.agora.capture.video.camera.VideoCaptureFrame;

public class PreprocessorHTEffect implements IPreprocessor {
    private final static String TAG = PreprocessorHTEffect.class.getSimpleName();

    private boolean renderSwitch;
    private int skipFrame = 0;

    private Handler mGLHandler;
    private final Context mContext;

    //todo --- hteffect start
    public static boolean isFrontCamera = true;
    public static boolean isCameraSwitched = false;
    public static boolean isRenderInit = false;
    //todo --- hteffect end

    public PreprocessorHTEffect(Context context) {
        mContext = context;
        renderSwitch = true;
    }

    @Override
    public VideoCaptureFrame onPreProcessFrame(VideoCaptureFrame outFrame, VideoChannel.ChannelContext context) {
        if (!renderSwitch) {
            return outFrame;
        }
        //todo --- hteffect start
        if (isCameraSwitched) {
            HTEffect.shareInstance().releaseTextureOESRenderer();
            isRenderInit = false;
            isCameraSwitched = false;
        }
        if (!isRenderInit) {
            isRenderInit = HTEffect.shareInstance().initTextureOESRenderer(outFrame.format.getWidth(), outFrame.format.getHeight(),
                isFrontCamera ? HTRotationEnum.HTRotationClockwise270 : HTRotationEnum.HTRotationClockwise90,
                isFrontCamera, 5);
        }
        outFrame.textureId = HTEffect.shareInstance().processTextureOES(outFrame.textureId);
        //todo --- hteffect end

        outFrame.format.setTexFormat(GLES20.GL_TEXTURE_2D);


        if (mGLHandler == null) {
            startGLThread();
        }
        if (skipFrame > 0) {
            skipFrame--;
            outFrame.textureId = 0;
            return outFrame;
        }
        return outFrame;
    }


    @Override
    public void initPreprocessor() {
        // only call once when app launched
        Log.e(TAG, "initPreprocessor: ");
    }

    @Override
    public void enablePreProcess(boolean enabled) {
        Log.e(TAG, "enablePreProcess: ");
    }

    @Override
    public void releasePreprocessor(VideoChannel.ChannelContext context) {
        // not called
        Log.d(TAG, "releasePreprocessor: ");
        //todo --- toivan start
        HTEffect.shareInstance().releaseTextureOESRenderer();
        isRenderInit = false;
        //todo --- toivan end
    }


    /* 创建线程  */
    private void startGLThread() {
        if (mGLHandler == null) {
            mGLHandler = new Handler(Looper.myLooper());
            mGLHandler.post(() -> {if (mSurfaceViewListener !=null ) mSurfaceViewListener.onSurfaceCreated();});
        }
    }

    public void doGLAction(Runnable runnable) {
        if (mGLHandler != null) {
            mGLHandler.post(runnable);
        }
    }

    public void setRenderEnable(boolean enabled) {
        renderSwitch = enabled;
    }


    public void skipFrame() {
        skipFrame = 5;
    }

    public void releaseFURender() {
        renderSwitch = false;
        mGLHandler.removeCallbacksAndMessages(0);
        mGLHandler.post(() -> {
            if (mSurfaceViewListener !=null ) mSurfaceViewListener.onSurfaceDestroyed();
        });
        mGLHandler = null;
    }

    private SurfaceViewListener mSurfaceViewListener;

    public interface SurfaceViewListener{
        void onSurfaceCreated();
        void onSurfaceDestroyed();
    }

    public void setSurfaceListener(SurfaceViewListener surfaceViewListener) {
        this.mSurfaceViewListener = surfaceViewListener;
    }

    //
}
